package com.glasses.business.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.entity.Address;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.applibrary.numberpicker.entity.CityResult;
import net.neiquan.applibrary.numberpicker.view.AlertCityPicker;
import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.RegexValidateUtil;
import org.haitao.common.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/8
 * Version：1.0
 */

public class AddrEditFragment extends AppBaseFragment {
    @InjectView(R.id.addressName)
    EditText addressName;
    @InjectView(R.id.addressPhone)
    EditText addressPhone;
    @InjectView(R.id.addressPlace)
    TextView addressPlace;
    @InjectView(R.id.addressDetail)
    EditText addressDetail;
    @InjectView(R.id.defaultImg)
    ImageView defaultImg;
    @InjectView(R.id.addressDefault)
    RelativeLayout addressDefault;
    private int status = 0;
    private Address address;
    private int id = 0;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_addr_edit;
    }

    @Override
    public void setViews() {
        setTitleTv("收货地址");
        setNextTv_("保存");
        setNextOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String addrName = addressName.getText().toString();
                String addrPhone = addressPhone.getText().toString();
                String addrPlace = addressPlace.getText().toString();
                String addrDetail = addressDetail.getText().toString();
                if (TextUtils.isEmpty(addrName)) {
                    ToastUtil.shortShowToast("请先输入收货人姓名...");
                } else if (!RegexValidateUtil.checkCellphone(addrPhone)) {
                    ToastUtil.shortShowToast("请输入正确的手机号码...");
                } else if (TextUtils.isEmpty(addrPlace)) {
                    ToastUtil.shortShowToast("请先选择所在地...");
                } else if (TextUtils.isEmpty(addrDetail)) {
                    ToastUtil.shortShowToast("请先输入详细地址...");
                } else {
                    Tools.showDialog(getActivity());
                    NetUtils.getInstance().addAddress(id, addrPlace, addrDetail, addrPhone, addrName, status, new NetCallBack() {
                        @Override
                        public void onSuccess(String response, String mssage, ResultModel model) {
                            Tools.dismissWaitDialog();
                            ToastUtil.shortShowToast(mssage);
                            EventBus.getDefault().post(new BussEvent(BussEvent.Save_Addr));
                            MyApplication.getInstance().manager.onBackPressed();
                        }

                        @Override
                        public void onFail(boolean dataError, int errorCode, String mssage) {
                            Tools.dismissWaitDialog();
                            ToastUtil.shortShowToast(mssage);
                        }
                    }, null);
                }
            }
        });
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            address = ((Address) getArguments().getSerializable("address"));
            addressName.setText(address.getReceiver());
            addressPhone.setText(address.getPhone());
            addressPlace.setText(address.getAddress());
            addressDetail.setText(address.getDetailAddress());
            status = address.getStatus();
            id = address.getId();
            switch (status) {
                case 0:
                    defaultImg.setImageResource(R.mipmap.icon_kgg);
                    break;
                case 1:
                    defaultImg.setImageResource(R.mipmap.icon_kg);
                    break;
            }
        }
    }

    @OnClick({R.id.addressPlace, R.id.addressDefault})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addressPlace:
                new AlertCityPicker.Builder(getActivity())
                        .setTitle("地区")
                        .setOnCitySelectListener(new AlertCityPicker.OnCitySelectListener() {
                    @Override
                    public void endSelect(CityResult cityResult, boolean isCancel) {
                        addressPlace.setText(cityResult.getProviceName() + cityResult.getCityName() + cityResult.getRegionName());
                    }
                }).show();
                break;
            case R.id.addressDefault:
                switch (status) {
                    case 0:
                        status = 1;
                        defaultImg.setImageResource(R.mipmap.icon_kg);
                        break;
                    case 1:
                        status = 0;
                        defaultImg.setImageResource(R.mipmap.icon_kgg);
                        break;
                }
                break;
        }
    }


    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
